import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import Goodshearder from './goodshearder'
import CommonJs from '../../utils/common'
import './my.scss'

export default class Index extends Component {
  constructor(props) {  
  	super(props);
    this.state = {
        lesstime:60,
        codestatus:'获取验证码',
        mobile:"",
        verifycode:"",
        newPassword:"",
        byfrom:''
    }
  }
  config = {
    navigationBarTitleText: '支付密码设置'
  }

  componentWillMount () {
		var type=this.$router.params.type
		if(type=='loginpwd'){
			Taro.setNavigationBarTitle({
			title:'登录密码设置'
		  })
		}
		this.setState({
	    byfrom : type
	  });
  }
  getcode(){
    let vm=this.state;
    if(vm.mobile==""){
      CommonJs.msg('输入的手机号不能为空');
      return false;   
  }
  if(!(/^1[123456789]\d{9}$/.test(vm.mobile))){
      CommonJs.msg('输入的手机号格式不正确');
      return false;       
  }       
  if(this.state.codestatus=="获取验证码"){
      if (this.state.lesstime != 60) {
          return false;
      }
      var value=this.state.lesstime;
      let _this=this;
      var codetimer = setInterval(function() {
      value=value-1;
          _this.setState({
            lesstime: value,
            codestatus:value+"秒后再获取"
          });
          if (value == 0) {
              clearInterval(codetimer);
              _this.setState({
                lesstime: 60,
                codestatus:"获取验证码"
              });
          }
      }, 1000) 
        var url='/hxsaccount/getMobileVerifyCode?mobile='+vm.mobile
        var _this=this;
        CommonJs.ajaxRequest(url,'','GET',function(ret){
          CommonJs.msg('验证码发送成功，请注意查收')
      })
  }  
  else{
      CommonJs.msg('验证码发送中')
  }
}
    sure(){
        var vm=this.state;
        if(vm.mobile=="" || vm.mobile==null){
			CommonJs.msg('输入的手机号不能为空');
	        return false;   
        }
        if(vm.verifycode =="" || vm.verifycode ==null){
			CommonJs.msg('输入的手机号验证码不能为空');
	        return false;   
        }
        if(vm.newPassword =="" || vm.newPassword ==null){
			CommonJs.msg('输入新的密码不能为空');
	        return false;   
        }
        var date ={
            mobile: vm.mobile,
            verifycode: vm.verifycode,
            newPassword: vm.newPassword
        }
        var url='/crm/papi/v1.9/currency/updateFundsPassword'
        CommonJs.ajaxRequest(url,date,'POST',function(ret){
            CommonJs.msg('修改成功')
            setTimeout(function () {
              CommonJs.open('/pages/my/homeset')
              },1000)
        },'wfw')
    }
    handleChange(name,e){
        var _this = this
          let value=e.target.value;
          this.setState({
          [name]: value
        });
      }

  change(){
  	 var vm=this.state;
	    if(vm.mobile=="" || vm.mobile==null){
				CommonJs.msg('旧密码不能为空');
	        return false;   
	    }
	    if(vm.verifycode =="" || vm.verifycode ==null){
			CommonJs.msg('请再次输入新密码');
	        return false;   
        }
        if(vm.newPassword =="" || vm.newPassword ==null){
			CommonJs.msg('输入新的密码不能为空');
	        return false;   
        }
        if(vm.newPassword != vm.verifycode){
				CommonJs.msg('二次密码不一样');
	        return false;   
        }
        var date ={
            oldPwd: vm.mobile,
            newPwd: vm.newPassword
        }
        var url='/user/updatePassword'
        CommonJs.ajaxRequest(url,date,'GET',function(ret){
            CommonJs.msg('修改成功')
            setTimeout(function () {
              CommonJs.open('/pages/my/homeset')
            },1000)
        })
  }
  render () {

    return (
      <View className='page'>
          <View className='pwd-set' style={this.state.byfrom=='loginpwd'?'display:none':''}>
          	<View className='pwd-list'>
              <View className='pwd-li'>
                <Input type='text' placeholder='请输入手机号' focus onInput={this.handleChange.bind(this,'mobile')} value={this.state.mobile}/>
                </View>
                <View className='pwd-lis' onClick={this.getcode.bind(this)}  >
                    <Text className='pwd-text'>{this.state.lesstime == 60 ? '获取验证码' : this.state.lesstime}</Text> 
                 </View>
            </View>
            <View className='pwd-list'>
                <View className='pwd-li'>
                <Input type='text' placeholder='请输入验证码' focus onInput={this.handleChange.bind(this,'verifycode')} value={this.state.verifycode}/>
                </View>
            </View>
            <View className='pwd-list'>
                <View className='pwd-li'>
                <Input type='text' placeholder='请输入新的支付密码' focus onInput={this.handleChange.bind(this,'newPassword')} value={this.state.newPassword} />
                </View>
            </View>
            <View className='zr-btn'>
        			<View className='zr-sure' onClick={this.sure.bind(this)}>确定</View>
        		</View>
          </View>
          
          <View className='pwd-set' style={this.state.byfrom=='loginpwd'?'':'display:none'}>
          		<View className='pwd-list'>
	                <View className='pwd-li'>
	                <Input type='password' placeholder='请输入旧密码' focus onInput={this.handleChange.bind(this,'mobile')}  />
	                </View>
	            </View>
	            <View className='pwd-list'>
	                <View className='pwd-li'>
	                <Input type='password' placeholder='请输入新密码' focus onInput={this.handleChange.bind(this,'newPassword')}  />
	                </View>
	            </View>
	            <View className='pwd-list'>
	                <View className='pwd-li'>
	                <Input type='password' placeholder='请再次输入新密码' focus onInput={this.handleChange.bind(this,'verifycode')}  />
	                </View>
	            </View>
	            <View className='zr-btn'>
	        			<View className='zr-sure' onClick={this.change.bind(this)}>确定</View>
	        		</View>
          </View>
      </View>
    )
  }
}

 